package com.itheima.service.impl;

import com.itheima.mapper.BrandMapper;
import com.itheima.pojo.Brand;
import com.itheima.pojo.PageBean;
import com.itheima.service.BrandService;
import com.itheima.util.SqlSessionFactoryUtils;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.junit.Test;

import java.util.List;

/**
 * @author Tensin
 * @version 1.0
 * @date
 */
public class BrandServiceImpl implements BrandService {

    //创建工厂对象
    SqlSessionFactory sqlSessionFactory = SqlSessionFactoryUtils.getSqlSessionFactory();

    //查询所有的品牌
    @Override
    public List<Brand> selectAll() {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        List<Brand> brands = mapper.selectAll();

        //释放资源
        sqlSession.close();

        return brands;
    }

    //新增品牌
    @Override
    public void add(Brand brand) {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.add(brand);

        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }

    //删除单个品牌
    @Override
    public void deleteSingle(int id) {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteSingle(id);

        //提交事务
        sqlSession.commit();

        sqlSession.close();

    }

    //批量删除
    @Override
    public void deleteByIds(int[] ids) {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        mapper.deleteByIds(ids);

        //提交事务
        sqlSession.commit();

        sqlSession.close();
    }


    //分页查询
    @Override
    public PageBean<Brand> selectByPage(int currentPage, int pageSize) {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int Begin = (currentPage - 1) * pageSize;
        int Num = pageSize;

        List<Brand> rows = mapper.selectByPage(Begin,Num);

        int totalCount = mapper.selectCountTotal();

        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //释放资源
        sqlSession.close();

        return pageBean;
    }

    //条件查询
    @Override
    public PageBean<Brand> selectByPageAndCondition(int currentPage, int pageSize, Brand brand) {
        //获取session对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        BrandMapper mapper = sqlSession.getMapper(BrandMapper.class);

        int Begin = (currentPage - 1) * pageSize;
        int Num = pageSize;
//写法1：
//        String brandName = brand.getBrandName();
//        if (brandName!=null && brandName.length()>0){
//            brand.setBrandName("%"+brandName+"%");
//        }
//
//        String companyName = brand.getCompanyName();
//        if (companyName!=null && companyName.length()>0){
//            brand.setCompanyName("%"+companyName+"%");
//        }


        List<Brand> rows = mapper.selectByPageAndCondition(Begin,Num,brand);

        int totalCount = mapper.selectCountTotalByCondition(brand);


        PageBean<Brand> pageBean = new PageBean<>();
        pageBean.setRows(rows);
        pageBean.setTotalCount(totalCount);

        //释放资源
        sqlSession.close();

        return pageBean;
    }
}
